package com.mmall.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.mmall.beans.MenuTreeResponse;
import com.mmall.common.JsonData;
import com.mmall.common.dtree.DTree;
import com.mmall.common.dtree.DTreeResponse;
import com.mmall.common.dtree.Status;
import com.mmall.model.SysMenu;
import com.mmall.param.MenuParam;
import com.mmall.service.SysMenuService;

/**
 * 系统菜单controller
 * Created by gameloft9 on 2017/12/4.
 */
@Controller
@Slf4j
@RequestMapping("/sys/menu")
public class SysMenuController {
	
    @Autowired
    SysMenuService sysMenuService;

    /**
     * 跳转至菜单界面
     * @return
     */
    @RequestMapping("/menu.page")
    public ModelAndView page() {
        return new ModelAndView("/sys/menu/menuList");
    }
    
    @RequestMapping("/details.page")
    public ModelAndView details() {
        return new ModelAndView("/sys/menu/menuEdit");
    }
    
    @RequestMapping("/menuSave.page")
    public ModelAndView saveMenuPage() {
        return new ModelAndView("/sys/menu/menuAdd");
    }
    
    @RequestMapping("/menuEdit.page")
    public ModelAndView menuEditPage() {
        return new ModelAndView("/sys/menu/menuEdit");
    }
    
    /**
     * 展示菜单树
     * @return
     */
    @RequestMapping("/tree.json")
    @ResponseBody
    public DTreeResponse tree() {
    	DTreeResponse resp = new DTreeResponse();
    	List<DTree> dtoList = sysMenuService.menuTree();
    	resp.setStatus(new Status());
    	resp.setData(dtoList);
    	return resp;
    }
    
    @RequestMapping("/listing.json")
	@ResponseBody
	public JsonData MenuListing(){
    	List<MenuTreeResponse> dtoList = sysMenuService.menuListing();
    	return JsonData.success(dtoList);
	}
    
    /**
     * 根据菜单标识获取菜单信息
     * @param menuId
     * @return
     */
    @RequestMapping("/details.json")
    @ResponseBody
    public JsonData details(@RequestParam("id") String id) {
    	SysMenu sysMenu = sysMenuService.getById(id);
    	if(null == sysMenu){
    		return JsonData.fail("未找到菜单数据");
    	}
        return JsonData.success(sysMenu);
    }
    
	@RequestMapping("/save.json")
	@ResponseBody
	public JsonData saveMenu(MenuParam param){
		sysMenuService.save(param);
        return JsonData.success(null,"菜单新增成功");
	}
	
	@RequestMapping("/update.json")
	@ResponseBody
	public JsonData updateMenu(MenuParam param){
		sysMenuService.update(param);
		return JsonData.success(null,"菜单修改成功");
	}
	
	@RequestMapping("/delete.json")
	@ResponseBody
	public JsonData deleteMenu(@RequestParam("menuId") String menuId){
		sysMenuService.delete(menuId);
		return JsonData.success(null,"菜单删除成功");
	}

}
